<template>
  <view class="container">
    <view class="bg">
      <image class="avatar" :src="info.avatar ? info.avatar_path : '/static/images/default_avatar.png'" mode="aspectFill"></image>
      <view class="nickname">{{ info.nickname  }}</view>
    </view>

    <view class="info-box">
      <view class="row">
        <view class="left">
          <image src="/static/images/tel.png"></image>
          <text>手机</text>
        </view>
        <view class="right">
          <text>{{ info.mobile }}</text>
          <image class="go" src="/static/images/go.png"></image>
        </view>
      </view>
      <navigator hover-class="none" open-type="switchTab" url="/pages/order_list/order_list" class="row" style="margin-bottom: 20rpx">
        <view class="left">
          <image src="/static/images/order.png"></image>
          <text>我的订单</text>
        </view>
        <image class="go" src="/static/images/go.png"></image>
      </navigator>
      <button open-type="contact" plain hover-class="none" class="contact-btn row">
        <view class="left">
          <image src="/static/images/contact.png"></image>
          <text>联系客服</text>
        </view>
        <image class="go" src="/static/images/go.png"></image>
      </button>
    </view>

    <view class="info-box" style="margin-top: 40rpx">
      <view class="row" @click="goPlan">
        <view class="left">
          <image src="/static/images/plan.png"></image>
          <text>我的任务</text>
        </view>
        <image class="go" src="/static/images/go.png"></image>
      </view>

      <navigator hover-class="none" :url="'/pages/apply/apply'" class="apply-box" v-if="info.examine_status == 0 && !info.user_extend.id">
        <text>申请成为陪诊员</text>
        <view class="btn">立即申请</view>
      </navigator>
      <navigator hover-class="none" :url="'/pages/apply/apply'" class="apply-box" v-else-if="info.examine_status == 0 && info.user_extend.id > 0">
        <text>申请审核中...</text>
        <view class="btn">查看申请</view>
      </navigator>
      <navigator hover-class="none" :url="'/pages/apply/apply'" class="apply-box" v-else-if="info.examine_status == 1">
        <text>陪诊员申请已通过</text>
        <view class="btn">立即查看</view>
      </navigator>
      <navigator hover-class="none" :url="'/pages/apply/apply'" class="apply-box" v-else-if="info.examine_status == 2">
        <text>申请被驳回：{{ info.refuse_reason}}</text>
        <view class="btn">修改申请</view>
      </navigator>
    </view>

    <view id="mask" v-if="show_login" @click.stop>
      <view class="mask-content" @click.stop>
        <form @submit="formSubmit" class="main-content">
          <view class="title">完善资料</view>
          <button open-type="chooseAvatar" @chooseavatar="getAvatar" class="avatar-btn">
            <image class="btnImg" :src="avatar_path" mode="aspectFill"></image>
          </button>
          <input type="text" :value="avatar" name="avatar" style="display: none">

          <view class="row">
            <view class="left">昵称</view>
            <input type="nickname" @focus="this.showLoginModel = true" @input="this.showLoginModel = true" :value="username" name="nickname" class="input" placeholder="请输入昵称">
          </view>

          <view class="row" style="border-top: none">
            <view class="left">手机号</view>
            <button open-type="getPhoneNumber" :plain="true" @getphonenumber="getPhoneNumber" class="mobile-btn" v-if="!mobile">获取手机号</button>
            <block v-else>
              <input type="number" :value="mobile" name="mobile" style="display: none">
              <view class="input">{{mobile}}</view>
            </block>
          </view>

          <view class="agree-box">
            <button hover-class="none" :type="'mini'" :plain="true" class="item agree-btn" open-type="agreePrivacyAuthorization">
              <checkbox :checked="is_agree" @click="doAgree" class="checkbox round color" color="#32A4FF" style="background: #FFF;border: none"/>
            </button>

            <view class="box">
              阅读并接受
              <text class="text" @click="openPrivacy">{{ privacyContractName }}</text>
            </view>
          </view>

          <input type="text" :value="openid" name="openid" style="display: none">

          <view class="btn-box">
            <button :plain="true" class="button cancel" @click="this.show_login = false">取消</button>
            <button form-type="submit" class="button save">保存</button>
          </view>
        </form>
      </view>
    </view>

  </view>
</template>

<script>
import http from '@/utils/http'
export default {
  data() {
    return {
      info: {},
      show_login: false,
      privacyContractName: '《陪诊预约隐私保护指引》',
      is_agree: false,
      avatar: 0,
      avatar_path: '../../static/images/default_avatar.png',
      username: '',
      mobile: '',
      showLoginModel: false,
      openid: '',
    };
  },

  methods: {
    goPlan: function (){
      var info = this.info;

      if(info.type == 2){
        uni.navigateTo({
          url: '/pages/plan_list/plan_list'
        })
      }else{
        uni.showToast({
          title: '请申请成为陪诊员',
          icon: 'none'
        })
      }
    },

    openPrivacy: function () {
      uni.openPrivacyContract()
    },

    getAvatar: function (e) {
      var {avatarUrl} = e.detail;

      uni.uploadFile({
        url: http.config.baseURL + '/Upload/UploadFile/uniUploadPicture', //服务端图片上传接口地址
        filePath: avatarUrl,
        name: 'download',
        formData: {},
        success: res => {
          res = JSON.parse(res.data)
          if (res.result === 1) {
            uni.showToast({
              title: '上传头像成功'
            })
            this.showLoginModel = true;
            this.avatar_path = res.data.path;
            this.avatar = res.data.id;
          } else {
            uni.showToast({
              title: res.msg,
              icon: 'none'
            })
          }
        },
        fail: err => {
          setTimeout(() => {
            uni.hideLoading();
            uni.showToast({
              title: '图片上传失败',
              icon: 'none'
            })
          }, 500)
        }
      })
    },

    getPhoneNumber: function (e){
      var code = e.detail.code;
      this.$http.post('/index/Api/getMobile', {code}).then(res => {
        if (res.data.result === 1) {
          this.mobile = res.data.data
        }else{
          this.$common.doErrorResult(res)
        }
      })
    },

    doAgree: function () {
      this.is_agree = !this.is_agree;
    },

    userCenter: function (user_id) {
      this.$http.post('/index/Api/userCenter', {user_id}).then(res => {
        if (res.data.result === 1) {
          this.info = res.data.data
        } else {
          this.$common.doErrorResult(res);
        }
      })
    },

    formSubmit: function (e) {
      var formData = e.detail.value,
        timestamp = Date.parse(new Date()),//该值除以1000就是当前的时间戳,
        sync_timestamp = uni.getStorageSync('timestamp') ? uni.getStorageSync('timestamp') : 0,//获取时间戳缓存,
        now_timestamp = timestamp / 1000,
        long_time = now_timestamp - sync_timestamp;//距离上次点击按钮经过的时长（秒）,

      if (!this.is_agree) {
        uni.showToast({
          title: '请阅读并勾选小程序隐私保护指引',
          icon: 'none'
        })
        return;
      }

      uni.showLoading({
        title: '保存中...',
        mask: true
      })

      if (long_time > 3) {
        //将当前时间戳存为缓存
        uni.setStorageSync('timestamp', timestamp / 1000);

        uni.login({
          success: result => {
            var code = result.code;

            this.$http.post('/index/Api/getOpenid', {code}).then(res => {
              if (res.data.result === 1) {
                formData.openid = res.data.data;

                this.$http.post('/Index/Api/login', formData).then(res => {
                  uni.removeStorageSync('timestamp');
                  uni.hideLoading();

                  if (res.data.result === 1) {
                    var user_id = res.data.data;
                    uni.setStorageSync('user_id', user_id)

                    this.show_login = false;

                    this.userCenter(user_id)
                  } else {
                    this.$common.doErrorResult(res);
                  }
                })
              } else {
                this.$common.doErrorResult(res);
              }
            })
          }
        })
      }
    },

  },

  onShow: function (){
    var user_id = uni.getStorageSync('user_id') ? uni.getStorageSync('user_id') : 0;

    if (!user_id) {
      uni.showLoading({
        title: '自动登录中...',
        mask: true
      })

      uni.login({
        success: result => {
          var code = result.code;

          this.$http.post('/index/Api/openidToUserid', {code}).then(res => {
            uni.hideLoading();
            if (res.data.result === 1) {
              var user_id = res.data.data;
              uni.setStorageSync('user_id', user_id)

              this.userCenter(user_id)
            } else {
              this.show_login = true;
            }
          })
        }
      })
    } else {
      this.userCenter(user_id)
    }
  },

}
</script>

<style lang="scss">
.container {
  padding: 0 20rpx;

  .bg {
    background-image: url("");
    background-size: cover;
    background-repeat: no-repeat;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: 370rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    z-index: -1;

    .avatar {
      width: 160rpx;
      height: 160rpx;
      border-radius: 50%;
      margin: 50rpx 0;
    }

    .nickname {
      font-size: 32rpx;
      color: #fff;
      font-weight: bold;
    }
  }

  .info-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    background: #fff;
    padding: 40rpx 30rpx;
    width: calc(100% - 60rpx);
    margin-top: 330rpx;
    border-radius: 20rpx;
    z-index: 99;

    .row {
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 100%;
      margin-bottom: 40rpx;

      &:last-child {
        margin-bottom: 0;
      }

      .left {
        display: flex;
        align-items: center;

        image {
          width: 30rpx;
          height: 30rpx;
          margin-right: 20rpx;
        }
      }

      .right{
        display: flex;
        align-items: center;
        text{
          margin-right: 10rpx;
          color: $main-blank;
        }
      }

      .go {
        width: 40rpx;
        height: 40rpx;
      }
    }

    .contact-btn {
      padding: 0;
      margin: 0;
      font-size: 28rpx;
      border: none;
    }

    .apply-box{
      display: flex;
      flex-direction: column;
      align-items: center;
      .btn{
        color: $main-color;
        border: 2rpx solid $main-color;
        border-radius: 40rpx;
        padding: 10rpx 60rpx;
        margin-top: 16rpx;
      }
    }
  }
}
</style>

